package com.sc.justification.system;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import com.sc.justification.dao.GenericDao;
import com.sc.justification.model.Document;
import com.sc.justification.model.JustificationCategory;

public class CategoryPanel extends JPanel {
	private JTable table;
	private JTable table_1;
	private List<JustificationCategory> list = new ArrayList<JustificationCategory>();
	private List<Document> docList = new ArrayList<Document>();
	private JPanel panel;
	private JButton btnNewButton;
	private final Action addCategoryAction = new SwingAction();
	private JButton btnNewButton_1;
	private final Action delDocAction = new SwingAction_1();
	private JButton btnNewButton_2;
	private JButton btnNewButton_3;
	private final Action action_2 = new SwingAction_2();
	private GenericDao<JustificationCategory> dao = new GenericDao<JustificationCategory>(
			JustificationCategory.class);
	private GenericDao<Document> docDao = new GenericDao<Document>(
			Document.class);
	private JDialog dialog;
	private JustificationCategory currentCategory;
	private JButton btnNewButton_4;
	private final Action addDocAction = new SwingAction_3();
	private JButton btnNewButton_5;
	private final Action delCategoryAction = new SwingAction_4();

	/**
	 * Create the panel.
	 */
	public CategoryPanel(final JDialog dialog) {
		delDocAction.setEnabled(false);
		this.dialog = dialog;
		list = ObservableCollections.observableList(dao.getAll());
		docList = ObservableCollections.observableList(docList);
		setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		add(panel, BorderLayout.SOUTH);

		btnNewButton = new JButton("New button");
		btnNewButton.setAction(addCategoryAction);
		panel.add(btnNewButton);

		btnNewButton_5 = new JButton("New button");
		btnNewButton_5.setEnabled(false);
		btnNewButton_5.setAction(delCategoryAction);

		panel.add(btnNewButton_5);

		btnNewButton_4 = new JButton("New button");
		btnNewButton_4.setAction(addDocAction);
		panel.add(btnNewButton_4);

		btnNewButton_1 = new JButton("New button");
		btnNewButton_1.setAction(delDocAction);
		panel.add(btnNewButton_1);

		btnNewButton_2 = new JButton("New button");
		btnNewButton_2.setAction(action_2);
		panel.add(btnNewButton_2);

		btnNewButton_3 = new JButton("退出");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog.setVisible(false);
			}
		});
		panel.add(btnNewButton_3);

		JSplitPane splitPane = new JSplitPane();
		add(splitPane, BorderLayout.CENTER);
		splitPane.setDividerLocation(200);

		table = new JTable();
		table.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting())
							return;
						docList.clear();
						if (table.getSelectedRow() != -1) {
							docList.addAll(list.get(table.getSelectedRow())
									.getDocuments());
						}
					}
				});
		JScrollPane scrollPane = new JScrollPane(table);
		splitPane.setLeftComponent(scrollPane);

		table_1 = new JTable();
		scrollPane = new JScrollPane(table_1);
		splitPane.setRightComponent(scrollPane);
		initDataBindings();

	}

	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "增加类别");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}

		public void actionPerformed(ActionEvent e) {
			JustificationCategory justification = new JustificationCategory();
			dao.save(justification);
			list.add(justification);
			EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					int row = list.size() - 1;
					table.setRowSelectionInterval(row, row);
					table.scrollRectToVisible(table.getCellRect(row, 0, true));
				}

			});
		}
	}

	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "删除文档");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}

		public void actionPerformed(ActionEvent e) {
			int i = table_1.getSelectedRow();
			docDao.delete(docList.get(i));
			docList.remove(i);
		}
	}

	private class SwingAction_2 extends AbstractAction {
		public SwingAction_2() {
			putValue(NAME, "保存");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}

		public void actionPerformed(ActionEvent e) {
			EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					if (table.getCellEditor() != null) {
						table.getCellEditor().stopCellEditing();
					}
					if (table_1.getCellEditor() != null) {
						table_1.getCellEditor().stopCellEditing();
					}
					for (JustificationCategory category : list) {
						dao.update(category);
					}
					for (Document doc : docList) {
						docDao.update(doc);
					}
				}

			});

		}
	}

	private class SwingAction_3 extends AbstractAction {
		public SwingAction_3() {
			putValue(NAME, "增加文档");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}

		public void actionPerformed(ActionEvent e) {

			EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					int index = table.getSelectedRow();
					JustificationCategory category = list.get(table
							.convertRowIndexToModel(index));
					Document doc = new Document();
					doc.setJustificationCategory(category);
					docDao.save(doc);
					docList.add(doc);

					final int row = docList.size() - 1;
					System.out.println("row:" + row);
					System.out.println("rowcount:" + table_1.getRowCount());
					table_1.setRowSelectionInterval(row, row);
					table_1.scrollRectToVisible(table.getCellRect(row, 0, true));

				}

			});

		}
	}

	private class SwingAction_4 extends AbstractAction {
		public SwingAction_4() {
			putValue(NAME, "删除类别");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}

		public void actionPerformed(ActionEvent e) {
			int i = table.getSelectedRow();
			System.out.println(i + ":" + list.get(i));
			dao.delete(list.get(i));
			list.remove(i);
			EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					table.clearSelection();
				}

			});
		}
	}

	protected void initDataBindings() {
		JTableBinding<JustificationCategory, List<JustificationCategory>, JTable> jTableBinding = SwingBindings
				.createJTableBinding(UpdateStrategy.READ_WRITE, list, table);
		//
		BeanProperty<JustificationCategory, String> justificationCategoryBeanProperty = BeanProperty
				.create("name");
		jTableBinding.addColumnBinding(justificationCategoryBeanProperty)
				.setColumnName("name");
		//
		BeanProperty<JustificationCategory, String> justificationCategoryBeanProperty_1 = BeanProperty
				.create("shortName");
		jTableBinding.addColumnBinding(justificationCategoryBeanProperty_1)
				.setColumnName("shortname");
		//
		jTableBinding.bind();
		//
		JTableBinding<Document, List<Document>, JTable> jTableBinding_1 = SwingBindings
				.createJTableBinding(UpdateStrategy.READ, docList, table_1);
		//
		BeanProperty<Document, String> documentBeanProperty = BeanProperty
				.create("name");
		jTableBinding_1.addColumnBinding(documentBeanProperty).setColumnName(
				"docname");
		//
		BeanProperty<Document, String> documentBeanProperty_1 = BeanProperty
				.create("shortName");
		jTableBinding_1.addColumnBinding(documentBeanProperty_1).setColumnName(
				"shortname");
		//
		jTableBinding_1.bind();
		//
		BeanProperty<JTable, JustificationCategory> jTableBeanProperty = BeanProperty
				.create("selectedElement");
		AutoBinding<JustificationCategory, JustificationCategory, JTable, JustificationCategory> autoBinding = Bindings
				.createAutoBinding(UpdateStrategy.READ_WRITE, currentCategory,
						table, jTableBeanProperty);
		autoBinding.bind();
		//
		BeanProperty<JustificationCategory, List<Document>> srcProp = BeanProperty
				.create("documents");
		AutoBinding<List<Document>, List<Document>, JustificationCategory, List<Document>> autoBinding2 = Bindings
				.createAutoBinding(UpdateStrategy.READ_WRITE, docList,
						currentCategory, srcProp);
		autoBinding2.bind();

		Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE,
				table, ELProperty.create("${selectedElement!=null}"),
				delCategoryAction, BeanProperty.create("enabled")).bind();
		Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE,
				table, ELProperty.create("${selectedElement!=null}"),
				addDocAction, BeanProperty.create("enabled")).bind();
		Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE,
				table_1, ELProperty.create("${selectedElement!=null}"),
				delDocAction, BeanProperty.create("enabled")).bind();
	}
}
